package com.chance.cc.crawler.development.command.job.domain.douyin.api;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.development.scripts.douyin.api.DYDevicesRegisterScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author bx
 * @date 2020/12/6 0006 12:44
 * 抖音 周度采集
 */
public class DYDevicesRegisterScheduleJob {

    private static String domainId = "api-dy";
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);

    public static void main(String[] args) {

        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob dyCrawlerSchduler = dyCrawlerSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(dyCrawlerSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("抖音搜索定时采集");
        crawlerScheduleJob.setCrawlerKey(dyCrawlerSchduler.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return dyCrawlerSchduler;

    }

    /**
     * dy采集
     * @return
     */
    public static CrawlerJob dyCrawlerSchduler() {
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl("https://start/dy/devices/register")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite("api-device");

//        //初始 comment crawler request record
        String requestQueueName = "crawler-api-dy-api-devices-queue";
        CrawlerRequestRecord initCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("dy-search",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/domain/common/api/v1/dy/devices/init?" +
                        "dyDeviceQueue=dy_devices_info_gen&dyDeviceRegisterQueue="+requestQueueName)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",DYDevicesRegisterScript.site, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerJobThreadNumber(1)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .consoleResultPipeline(null)
                .requestRecord(requestRecord)
                .supportRecord(initCrawlerRecord)
                .restWhileNon(false)
                .build();
        return crawlerJob;
    }

}
